<template>
  <div>
      <!-- 头部 -->
       <div class="header">
       <i class="iconfont iconjiantou2" @click="backHandle"></i>
       <div class="input" @keyup.enter="searchHandle">
           <i class="iconfont iconsearch"></i>
           <input type="text" placeholder="通灵兽消失术" v-model="keyword" ref="search_input_dom">
       </div>
       <div class="search" @click="searchHandle">
           搜索
       </div>
   </div>
   <!-- 历史记录 -->
    <div class="history" v-show="!isShowTip">
        <div class="title">
            <h3>历史记录</h3>
            <span @click="removeHandle">清除记录</span>
        </div>
        <ul class="list" >
            <li class="item" v-for="(item,index) in history" :key="index" @click="listHandle(index)">{{item}}</li>
        </ul>
    </div>
    <!-- 热门搜索 -->
    <div class="hot" v-show="!isShowTip">
        <div class="hottitle" >
            <h3>热门搜索</h3>
        </div>
        <ul class="hotlist">
            <li class="item">办公室小野否认解散</li>
            <li class="item">月季如何嫁接</li>
            <li class="item">办公室小野否认解散</li>
            <li class="item">月季如何嫁接</li>
            <li class="item">办公室小野否认解散</li>
            <li class="item">月季如何嫁接</li>
        </ul>
    </div>
    <!-- 提示框 -->
    <div class="tips" v-show="isShowTip">
        <div class="tip_item" v-for="item in list" :key="item.id" @click="$router.push(`/detail/${item.id}`)">
            <span class="tip_title">{{item.title}}</span>
            <i class="iconfont iconjiantou1"></i>
        </div>
    </div>
  </div>
</template>

<script>
import { getPostsearch } from '@/api'
import {setHistory,getHistory,removeHistory} from '@/untails/local'
export default {
   data () {
       return {
           isShowTip:false,
           keyword:"",
           list:[],
           history:[]
       }
   },
   watch:{
       keyword(val){
           if(val===''){
               this.isShowTip=false
           }
       },
       history(val){
          setHistory(val)
       }
   },
   mounted(){
       this.$refs.search_input_dom.focus()
       this.history=getHistory()
   },
   methods:{
       searchHandle(){
        if (this.keyword === "") {
        return this.$toast("请输入内容哦...");
      }
           getPostsearch({
               keyword:this.keyword,
               pageSize:5
               }).then((res)=>{
               this.list=res.data.data
               this.isShowTip=true
               this.history.unshift(this.keyword)
               this.history = [...new Set(this.history)];
               
               
           })
           
       },
       backHandle(){
           if(this.keyword){
               this.keyword=''
           }else{
               this.$router.back()
           }
       },
       removeHandle(){
           this.history=[]
           removeHistory()
       },
       listHandle(index){
           this.keyword=this.history[index]
           this.searchHandle()
       }
   }
}
</script>

<style lang="less" scoped>
.header {
    height: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0 20px;
.iconjiantou2 {
   font-size: 16px;
   margin-right: 15px;
  }

.input {
   
    flex: 1;
   position: relative;
   display: flex;
   align-items: center;
  .iconsearch {
    position: absolute;
    top: 9px;
    left: 16px;
    }

    input {
        width: 230px;
         border-radius: 100px;
       padding: 8px;
     text-indent: 2em;
     border-color: #999;
    }
  }

}
.history {
    padding: 0 20px;
    .title{
        display: flex;
        align-items: center;
        justify-content: space-between;
        span{
            color: #999;
            font-size: 12px;
        }
    }
  .list {
      border-bottom: 2px solid #ccc;
      display: flex;
      flex-wrap: wrap;
      justify-content: space-between;
    .item {
      flex: 33.3%;
      margin: 10px 0;
    }
  }
}
.hot {
    .hottitle{
        margin: 10px 0;
    }
   padding: 0 20px;
  .hotlist {
      display: flex;
      flex-wrap: wrap;
    .item {
       flex: 50%;
       margin: 10px 0;
    }
  }
}

.tips {
    position: absolute;
    right: 0;
    left: 0;
    top: 44px;
    background-color: #fff;
    bottom: 0;
.tip_item {
   border-bottom: 1px solid #ccc;
     height: 50px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 10px;
  .tip_title {
       font-size: 16px;
    }
  }
}
</style>